package upmc.fr.android.rss.persistence;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import upmc.fr.android.model.Dictionary;
import android.app.Activity;
import android.content.Context;

public class FilePersistence 
{	
	private static final String CONFIGURATION_FILENAME = "data.cie";
	
	public static void persist(Dictionary dictionary, Activity currentActivity) throws IOException
	{		
		ObjectOutputStream oos = null;
		try
		{		
			oos = new ObjectOutputStream(currentActivity.openFileOutput(CONFIGURATION_FILENAME, Context.MODE_PRIVATE));			
			oos.writeObject(dictionary);			
			oos.flush();			
		}
		finally
		{			
			if(oos != null)
				oos.close();
		}
	}
	
	
	public static Dictionary load(Activity activity) throws IOException, ClassNotFoundException
	{		
		ObjectInputStream ois = null;
		Dictionary result = null;
		try
		{
			ois = new ObjectInputStream(activity.openFileInput(CONFIGURATION_FILENAME));
			
			
			result = (Dictionary)ois.readObject();
			
		}
		finally
		{
			if(ois != null)
				ois.close();
		}
		return result;
	}

}
